﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SimplIDE
{
    public partial class Options : Form
    {
        string dirpath;
        SyntaxHighlighter sh;
        string syntaxloc;

        public Options(string d)
        {
            InitializeComponent();
            panel1.Controls.SetChildIndex(checkBox1, 0);
            panel1.Controls.SetChildIndex(checkBox2, 1);
            panel1.Controls.SetChildIndex(checkBox3, 2);
            panel1.Controls.SetChildIndex(checkBox4, 3);
            panel1.Controls.SetChildIndex(checkBox5, 4);
            panel1.Controls.SetChildIndex(checkBox6, 5);
            panel1.Controls.SetChildIndex(checkBox7, 6);
            dirpath = d;
        }

        private void Options_Load(object sender, EventArgs e)
        {
            textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = "";
            textBox1.Enabled = textBox2.Enabled = textBox3.Enabled = false;
            StreamReader sr = new StreamReader(dirpath+@"\buildoptions.txt");
            string sor;
            int i = 0;
            while ((sor = sr.ReadLine()) != null && i<7)
            {
                if (sor != "0")
                {
                    CheckBox cb = (CheckBox)panel1.Controls[i];
                    cb.Checked = true;
                    switch (i)
                    {
                        case 4: textBox1.Text = sor; textBox1.Enabled = true; break;
                        case 5: textBox2.Text = sor; textBox2.Enabled = true; break;
                        case 6: textBox3.Text = sor; textBox3.Enabled = true; break;                        
                    }
                }
                else
                {
                    CheckBox cb = (CheckBox)panel1.Controls[i];
                    cb.Checked = false;
                }
                ++i;
            }
            if (sor != null)
            {
                textBox4.Text = sor;                
            }
            sr.Close();
            sr = new StreamReader(dirpath +@"\runoptions.txt");
            if ((sor = sr.ReadLine()) != null)
            {
                textBox5.Text = sor;
            }
            sr.Close();
            FileInfo fi = new FileInfo(Application.ExecutablePath);
            syntaxloc = fi.DirectoryName + @"\SyntaxHighlighterSettingsBlack.xml";
            sh = SyntaxHighlighter.Load(syntaxloc);
            lblBackground.ForeColor = sh.Colors.Background;
            lblTextColor.ForeColor = sh.Colors.Text;
            lblKeyWord.ForeColor = sh.Colors.Keyword;
            lblNumber.ForeColor = sh.Colors.Number;
            lblString.ForeColor = sh.Colors.String;
            lblComment.ForeColor = sh.Colors.Comment;
            lblIdentifier.ForeColor = sh.Colors.Identifier;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter(dirpath+@"\buildoptions.txt");
            for (int i = 0; i < 7; ++i)
            {
                CheckBox cb = (CheckBox)panel1.Controls[i];
                if (cb.Checked)
                {
                    switch (i)
                    {
                        case 4: sw.WriteLine(textBox1.Text); break;
                        case 5: sw.WriteLine(textBox2.Text); break;
                        case 6: sw.WriteLine(textBox3.Text); break;
                        default: sw.WriteLine("1"); break;
                    }
                }
                else
                {
                    sw.WriteLine("0");
                }
            }
            if (textBox4.Text != "") sw.WriteLine(textBox4.Text);
            sw.Close();
            sw = new StreamWriter(dirpath+@"\runoptions.txt");
            sw.WriteLine(textBox5.Text);
            sw.Close();           
            sh.Colors.BackgroundColor = ColorTranslator.ToHtml(lblBackground.ForeColor);
            sh.Colors.TextColor = ColorTranslator.ToHtml(lblTextColor.ForeColor);
            sh.Colors.KeywordColor = ColorTranslator.ToHtml(lblKeyWord.ForeColor);
            sh.Colors.NumberColor = ColorTranslator.ToHtml(lblNumber.ForeColor);
            sh.Colors.StringColor = ColorTranslator.ToHtml(lblString.ForeColor);
            sh.Colors.CommentColor = ColorTranslator.ToHtml(lblComment.ForeColor);
            sh.Colors.IdentifierColor = ColorTranslator.ToHtml(lblIdentifier.ForeColor);
            sh.Save(syntaxloc);
            this.Close();
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            textBox1.Enabled = !textBox1.Enabled;
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            textBox2.Enabled = !textBox2.Enabled;
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            textBox3.Enabled = !textBox3.Enabled;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

            textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = "";
            textBox1.Enabled = textBox2.Enabled = textBox3.Enabled = false;
            StreamReader sr = new StreamReader(dirpath+@"\buildoptions.txt");
            string sor;
            int i = 0;
            while ((sor = sr.ReadLine()) != null && i < 7)
            {
                if (sor != "0")
                {
                    CheckBox cb = (CheckBox)panel1.Controls[i];
                    cb.Checked = true;
                    switch (i)
                    {
                        case 4: textBox1.Text = sor; textBox1.Enabled = true; break;
                        case 5: textBox2.Text = sor; textBox2.Enabled = true; break;
                        case 6: textBox3.Text = sor; textBox3.Enabled = true; break;
                    }
                }
                else
                {
                    CheckBox cb = (CheckBox)panel1.Controls[i];
                    cb.Checked = false;
                }
                ++i;
            }
            if (sor != null)
            {
                textBox4.Text = sor;
            }
            sr.Close();            
            sr = new StreamReader(dirpath +@"\runoptions.txt");
            if ((sor = sr.ReadLine()) != null)
            {
                textBox5.Text = sor;
            }
            sr.Close();
            this.Close();
        }

        private void label4_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                lblBackground.ForeColor = colorDialog1.Color;               
            }
        }

        private void lblTextColor_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                lblTextColor.ForeColor = colorDialog1.Color;
            }
        }

        private void lblKeyWord_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                lblKeyWord.ForeColor = colorDialog1.Color;
            }
        }

        private void lblNumber_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                lblNumber.ForeColor = colorDialog1.Color;
            }
        }

        private void lblString_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                lblString.ForeColor = colorDialog1.Color;
            }
        }

        private void lblComment_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                lblComment.ForeColor = colorDialog1.Color;
            }
        }

        private void lblIdentifier_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                lblIdentifier.ForeColor = colorDialog1.Color;
            }
        }        
    }
}